package com.hnas.appLogin.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnas.api.dto.DTOResult;
import com.hnas.appLogin.dto.DTOCheckedVerifyCode;
import com.hnas.appLogin.dto.DTOCheckedVerifyCodeResult;
import com.hnas.appLogin.service.VerifyCodeService;
import com.hnas.common.domain.CommonStatus;
import com.hnas.common.interfacedsc.InterfaceDsc;
import com.hnas.common.util.CommonConstants;
import com.hnas.common.util.CommonUtils;
import com.hnas.common.util.ResultCode;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;

@Controller
@RequestMapping("/app/verifyCode")
public class VerifyCodeControll {
	
	private static Log log = LogFactory.getLog(VerifyCodeControll.class);
	
	
	@Value("#{component['verification_time']}")
	private String verificationTime;
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	@Autowired
	private VerifyCodeService verifyCodeService;
	
	/**
	  * 发送手机短信验证码
	  * @param dto
	  * @return
	  */
	 @ResponseBody
	 @RequestMapping(value="/verificationCode",method = RequestMethod.POST)
	 public String getRedPacket(HttpServletRequest request){
		 DTOResult dtoResult = new DTOResult();
			try {
				//String phoneNo,String type ModelMap model, HttpServletRequest request,HttpServletResponse response
				String sendData = request.getParameter("sendData");
				JSONObject jo = (JSONObject) JSON.parse(sendData);
				String phoneNo = jo.getString("phone_no");
				String type = jo.getString("verify_type");
				if(StringUtil.isEmpty(phoneNo)){
					dtoResult.setResultCode(ResultCode.ERROR_003);
					dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.ERROR_003));
					return objectMapper.writeValueAsString(dtoResult);
				}
				
				if(StringUtil.isEmpty(type)){
					dtoResult.setResultCode(ResultCode.ERROR_004);
					dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.ERROR_004));
					return objectMapper.writeValueAsString(dtoResult);
				}
				 String string = verifyCodeService.sendVerifyCode(phoneNo, type);
				if(null!=string){
					return string;
				}
				 dtoResult.setResultCode(ResultCode.ERROR_005);
			     dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.ERROR_005));
				 return objectMapper.writeValueAsString(dtoResult);
			} catch (Exception e) {
				 dtoResult.setResultCode(ResultCode.ERROR_005);
			     dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.ERROR_005));
				try {
					return objectMapper.writeValueAsString(dtoResult);
				} catch (JsonProcessingException e1) {
					e1.printStackTrace();
					return "";
				}
			}
	   }
	 
	 
	 /**
		 * 校验验证码
		 * @param request
		 * @return
		 */
		@RequestMapping(value = "/checkedVerifyCode", method = RequestMethod.POST)
		@ResponseBody
		public String checkedVerifyCode(HttpServletRequest request) {
			DTOResult dtoResult = new DTOResult();
			String sendData = "";
			DTOCheckedVerifyCode dto = null;
			try {
				sendData = request.getParameter("sendData");
				// 入参为空判断
				if (StringUtil.isEmpty(sendData)) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_10000, null);
				}
				dto = objectMapper.readValue(sendData, DTOCheckedVerifyCode.class);
			    //手机号码为空判断
				if (StringUtil.isEmpty(dto.getPhone_no())) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_003, null);
				}
				// 验证码判空
				if (StringUtil.isEmpty(dto.getVerify_code())) {
					return CommonUtils.createResult(dtoResult, ResultCode.ERROR_009, null);
				}
				
				boolean flag = verifyCodeService.checkVerifyCode(dto.getPhone_no(), dto.getVerify_code());
				DTOCheckedVerifyCodeResult result = new DTOCheckedVerifyCodeResult();
				if(flag){
					result.setResult(CommonStatus.TRUE);
				}else{
					result.setResult(CommonStatus.FALSE);
				}
				return CommonUtils.createResult(dtoResult, ResultCode.SUCCESS, objectMapper.writeValueAsString(result));
			} catch (Exception e) {
				e.printStackTrace();
				// 日志记录
				log.info(DateUtil.dateTimeToString(new Date()) + "调用接口：" + InterfaceDsc.APP_WXXNK_CheckedVerfyCode
						+ ",请求数据:入参为:" + sendData + "  出错:" + e);
				return CommonUtils.createResult(dtoResult, ResultCode.ERROR_114, null);
			} finally {
				// 日志记录
				log.info(DateUtil.dateTimeToString(new Date()) + "调用接口：" + InterfaceDsc.APP_WXXNK_CheckedVerfyCode
						+ ",请求数据:入参为:" + sendData + "  返回参数:" + dtoResult);
			}
		}
}
